/*
 * Bullet.h
 *
 *  Created on: 07.12.2010
 *      Author: 123
 */

#ifndef BULLET_H_
#define BULLET_H_
#include <stdlib.h>
#include <oslib/oslib.h>
#include "Colors.h"
#include "Char.h"
#include "sSight.h"
#include <stdlib.h>
extern sMap Map;

extern int CameraX;
extern int CameraY;
extern const float air_resist;
extern int NumBullets;
struct Vector {
	float x, y;
};

struct sBullet {

	Vector Start;
	Vector Dir;
	float angle;

	float speed;
	bool exist;
	float max_dist;
	float ang;
	int length;

};
extern sBullet Bullets[100];

class sBulletController {
	int i;
public:
	sBulletController() {
		Null();
	}
	void Null() {
		for (i = 0; i < 100; i++) {
			Bullets[i].exist = false;
		}
	}
	void Create(int amount, int x, int y, float angle, float speed,
			float maxdist, int length) {

		for (i = 0; i < 100; i++) {
			if (Bullets[i].exist == false && amount != 0) {
				Bullets[i].Start.x = x;
				Bullets[i].Start.y = y;
				Bullets[i].Dir.x = x;
				Bullets[i].Dir.y = y;

				Bullets[i].angle = angle;
				if (Char[0].Sight.GetSpread())
					Bullets[i].ang = ((rand() % 2 == 1) ? (angle - rand()
							% Char[0].Sight.GetSpread()) : (angle + rand()
							% Char[0].Sight.GetSpread())) * 3.14 / 180;
				else
					Bullets[i].ang = ((rand() % 2 == 1) ? (angle) : (angle))
							* 3.14 / 180;
				Bullets[i].speed = speed;
				Bullets[i].exist = true;
				Bullets[i].max_dist = maxdist;
				Bullets[i].length = length;
				amount--;

			} else if (amount == 0)
				break;
		}

	}
	;
	void Draw() {
		for (i = 0; i < 100; i++) {
			if (Bullets[i].exist == true) {
				if (Bullets[i].speed > 0.5 && Bullets[i].max_dist > sqrt(
						abs(
								(pow(Bullets[i].Dir.x, 2) + pow(
										Bullets[i].Dir.y, 2)) - (pow(
										Bullets[i].Start.x, 2) + pow(
										Bullets[i].Start.y, 2))))) {
					Bullets[i].speed = Bullets[i].speed / air_resist;

				}

				else {
					Bullets[i].exist = false;

				}
				int speed = 0;
				while (1) {
					if (Map.GetAbility(Bullets[i].Dir.x / 32,
							Bullets[i].Dir.y / 32))
						Bullets[i].exist = false;

					Bullets[i].Dir.x += 32 * cos(Bullets[i].ang);
					Bullets[i].Dir.y += 32 * sin(Bullets[i].ang);
					speed += 32;
					if (speed >= Bullets[i].speed)
						break;
					oslDrawLine(
							Bullets[i].Dir.x - CameraX,
							Bullets[i].Dir.y - CameraY,
							Bullets[i].Dir.x - CameraX + Bullets[i].length
									* cos(Bullets[i].ang),
							Bullets[i].Dir.y - CameraY + Bullets[i].length
									* sin(Bullets[i].ang), RED);

				}

			}
		}
	}

};
extern sBulletController Bullet;

#endif /* BULLET_H_ */
